package ee.metadataUtils.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import ee.metadataUtils.ObjectFactory;

public class ObjectFactoryImpl implements ObjectFactory {
	public ObjectFactoryImpl()
	{
	}

	@Override
	public Object create(Type type) throws InstantiationException {
		
		Type rawType = type;
		
		if (type instanceof ParameterizedType)
		{
			rawType = ((ParameterizedType) type).getRawType();
		}
		
		if (rawType instanceof Class)
		{
			try {
				return ((Class<?>) rawType).newInstance();
			} catch (IllegalAccessException e) {
				throw new InstantiationException("Illigal access exception: " + e.getMessage());
			}
		} else
		{
			throw new InstantiationException("Can only create instances of types that are instances of Class or are ParameterizedTypes that have a raw type that is an instance of Class");
		}
}
}
